package com.jiukuaidao.zhulin.module.address.presenter;

import com.jiukuaidao.zhulin.data.TasksRepository;
import com.jiukuaidao.zhulin.entity.ApiGeoCoderInfo;
import com.jiukuaidao.zhulin.entity.ApiPoiSearchInfo;
import com.jiukuaidao.zhulin.expand.baidu.ICallBack;
import com.jiukuaidao.zhulin.expand.baidu.webapi.Geocoding;
import com.jiukuaidao.zhulin.expand.baidu.webapi.PoiSearch;
import com.jiukuaidao.zhulin.module.BasePresenter;
import com.jiukuaidao.zhulin.module.address.contract.GainPlaceContract;
import com.ta.utdid2.android.utils.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/31
 * Description :
 */
public class GainPlacePresenter extends BasePresenter<GainPlaceContract.View, TasksRepository> implements GainPlaceContract.Presenter {
    public GainPlacePresenter(TasksRepository repository, GainPlaceContract.View view) {
        super(repository, view);
    }

    @Override
    public void start() {

    }

    //根据定位坐标进行反解析
    @Override
    public void startGeoCoderSearch() {
        Geocoding.geocoding(mView.getLongitude(), mView.getLatitude(), new ICallBack<ApiGeoCoderInfo>() {
            @Override
            public void onSuccess(ApiGeoCoderInfo apiGeoCoderInfo) {
                List<Map<String, String>> maps = new ArrayList<Map<String, String>>();
                if (apiGeoCoderInfo.result != null && apiGeoCoderInfo.result.pois != null) {
                    for (int i = 0; i < apiGeoCoderInfo.result.pois.size(); i++) {
                        Map<String, String> resultMap = new HashMap<String, String>();
                        resultMap.put("name", apiGeoCoderInfo.result.pois.get(i).name);
                        resultMap.put("address", apiGeoCoderInfo.result.pois.get(i).addr);
                        resultMap.put("city", apiGeoCoderInfo.result.addressComponent.city);
                        resultMap.put("cityCode", apiGeoCoderInfo.result.cityCode);
                        if (!StringUtils.isEmpty(apiGeoCoderInfo.result.pois.get(i).point.x) && !StringUtils.isEmpty(apiGeoCoderInfo.result.pois.get(i).point.y)) {
                            resultMap.put("latitude", apiGeoCoderInfo.result.pois.get(i).point.y);
                            resultMap.put("longitude", apiGeoCoderInfo.result.pois.get(i).point.x);
                        }
                        maps.add(resultMap);
                    }
                }
                mView.placeList(maps, "Geo");

            }

            @Override
            public void onFailed(String errorMsg) {

            }
        });

    }

    //根据关键字检索
    @Override
    public void startSuggestionSearch() {
        PoiSearch.search(mView.getQuery(), mView.getRegion(), new ICallBack<ApiPoiSearchInfo>() {
            @Override
            public void onSuccess(ApiPoiSearchInfo apiPoiSearchInfo) {
                if (apiPoiSearchInfo != null && apiPoiSearchInfo.status == 0) {
                    List<Map<String, String>> list = new ArrayList<Map<String, String>>();
                    List<ApiPoiSearchInfo.ResultsBean> resultList = apiPoiSearchInfo.results;
                    for (ApiPoiSearchInfo.ResultsBean result : resultList) {
                        if (result != null && result.location != null) {
                            Map<String, String> resultMap = new HashMap<String, String>();
                            resultMap.put("address", result.address);
                            resultMap.put("name", result.name);
                            resultMap.put("latitude", String.valueOf(result.location.lat));
                            resultMap.put("longitude", String.valueOf(result.location.lng));
                            resultMap.put("city", mView.getRegion());
                            resultMap.put("cityCode", mView.getCityCode());
                            list.add(resultMap);
                        }
                        mView.placeList(list, "Sug");
                    }

                }
            }

            @Override
            public void onFailed(String errorMsg) {

            }
        });
    }
}
